package leetCode.thread.leetCode1114;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 原子变量计数实现顺序执行，自旋
 */
public class Foo3 implements Foo {

    private final AtomicInteger count = new AtomicInteger(0);

    @Override
    public void first(Runnable printFirst) throws InterruptedException {
        printFirst.run();
        count.incrementAndGet();
    }

    @Override
    public void second(Runnable printSecond) throws InterruptedException {
       while (count.get()!=1){

       }
       printSecond.run();
       count.incrementAndGet();
    }

    @Override
    public void third(Runnable printThird) throws InterruptedException {
        while (count.get()!=2){

        }
        printThird.run();
        count.incrementAndGet();
    }

}
